<?php
/**
 * File keep Llib_List class
 *
 * @author Yaroslav Olekh
 */


/**
 * Llib_List class
 *
 */
class Llib_Cart extends SF_Model_Abstract //implements SeekableIterator, Countable, ArrayAccess
{
    protected  $_items = array();
    protected  $_total = 0;
    protected  $_count = 0;
    protected  $_sessionNamespace;

	public function init()
    {
       $this->loadSession();
    }

	public function addItem($item, $qty)
    {
        if (0 > $qty) {
               return false;
        }

        if (0 == $qty) {
            $this->removeItem($product);
            return false;
        }

        if(isset($this->_items[$item['id']]))
    	{
   	        $this->_items[$item['id']]['quantity'] += $qty;
       		$t = ($item['price']*$this->_items[$item['id']]['quantity']);
	        $item['nprice'] = number_format($t, 2, '.', '');
        	$this->_items[$item['id']]['nprice'] = $item['nprice'];
    	}
        else
        {
        	$item['quantity'] = $qty;
       		$t = ($item['price']*$qty);
	        //$item['nprice'] = mb_substr($t, 0, mb_strlen($t)-2).'.'.mb_substr($t, mb_strlen($t)-2, 2);
	        $item['nprice'] = number_format($t, 2, '.', '');
        	$this->_items[$item['id']] = $item;
        }

        $this->persist();

        return $item;
    }

	public function updateItem($item, $qty)
    {
        if (0 > $qty) {
               return false;
        }

        if (0 == $qty) {
            $this->removeItem($product);
            return false;
        }

        $item['quantity'] = $qty;

        if(isset($this->_items[$item['id']]))
    	{
   	        $this->_items[$item['id']]['quantity'] = $qty;

    		$t = floatval($item['price']*$qty);
	        //$item['nprice'] = mb_substr($t, 0, mb_strlen($t)-2).'.'.mb_substr($t, mb_strlen($t)-2, 2);
        	$this->_items[$item['id']]['nprice'] =  number_format($t, 2, '.', '');
    	}
        else $this->_items[$item['id']] = $item;

        $this->persist();

        return $item;
    }

	public function removeItem($id)
    {
       	unset($this->_items[$id]);
	    $this->persist();
    }

    public function removeAll()
    {
       	$this->_items = array();
	    $this->persist();
    }


	public function setSessionNs(Zend_Session_Namespace $ns)
    {
          $this->_sessionNamespace = $ns;
    }

    public function getSessionNs()
     {
           if (null === $this->_sessionNamespace) {
               $this->setSessionNs(new
                      Zend_Session_Namespace(__CLASS__));
            }
            return $this->_sessionNamespace;
     }

     public function persist()
     {
         $this->getSessionNs()->items = $this->_items;
     }

     public function loadSession()
     {
           if (isset($this->getSessionNs()->items)) {
                 $this->_items = $this->getSessionNs()->items;
           }
     }

	public function CalculateTotals()
    {
        $sub = 0;
        foreach ($this->_items as $item) {
             $sub = $sub + $item['price']*$item['quantity'];
        }
        $this->_total = $sub;
    }

	public function CalculateCount()
    {
        $sub = 0;
        foreach ($this->_items as $item) {
             $sub = $sub + $item['quantity'];
        }
        $this->_count = $sub;
    }

	public function getTotal()
    {
        $this->CalculateTotals();
        if($this->_total == 0) return 0;
        $this->_total = round($this->_total, 2);
        $t =   $this->_total*100;
        return number_format($this->_total, 2, '.', '');
    }

	public function getCount()
    {
        $this->CalculateCount();
        return $this->_count;
    }

    public function getItems()
    {
    	return $this->_items;
	}
}
